<template>
  <div class="container">
    <!-- 条件搜索框 -->
    <div class="input">
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="标题" prop="title">
          <el-input v-model="searchForm.title" placeholder="文章标题" clearable @clear="clear" />
        </el-form-item>
        <el-form-item label="作者" prop="username">
          <el-input v-model="searchForm.username" placeholder="作者名称" clearable @clear="clear" />
        </el-form-item>
        <el-form-item label="文章类型" prop="type">
          <el-select v-model="searchForm.articleType" clearable placeholder="请选择文章类型" @change="changeHandle">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="findArticleList">查询</el-button>
        </el-form-item>
        <el-form-item style="float:right">
          <el-button type="primary" @click="manageArticleType">管理文章类型</el-button>
        </el-form-item>
        <el-form-item style="float:right">
          <el-button type="primary" @click="dialogVisibleAdd = true">添加文章类型</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 列表 -->
    <el-table
      :data="tableData"
      stripe
      fit
      style="width: 100%"
    >
      <el-table-column
        prop="type"
        label="文章类型"
        width="150"
      >
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag v-for="type in scope.row.type" :key="type.id" size="medium" effect="dark" type="success">{{ type.name }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="article.title"
        label="标题"
      />
      <el-table-column
        prop="article.description"
        label="简介"
      />
      <el-table-column
        prop="username"
        label="作者"
      />
      <el-table-column
        fixed="right"
        label="操作"
        width="250"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="check(scope.row.article.id)">查看</el-button>
          <el-button type="primary" disabled size="small">编辑</el-button>
          <el-popconfirm
            :title="text1 + scope.row.username + text2"
            style="margin-left:10px"
            confirm-button-text="删除"
            cancel-button-text="取消"
            @confirm="del(scope.row.article.id)"
            @cancel="cancel"
          >
            <el-button slot="reference" size="small" type="primary">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      ref="page"
      background
      layout="prev, pager, next"
      :total="totalCount"
      @current-change="pageCurrentChange"
    />
    <el-dialog
      title="管理文章类型"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span>
        <el-tag v-for="tag in options" :key="tag.id" closable @close="delType(tag.id)">{{ tag.name }}</el-tag>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加文章类型"
      :visible.sync="dialogVisibleAdd"
      width="30%"
      :before-close="handleClose"
    >
      <span>
        <el-input v-model="addNewType" placeholder="请输入内容" />
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="addArticleType">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { findArticleList, getTypeListAll, delArticle, addArticleType, delArticleType } from '@/api/article'
export default {
  data() {
    return {
      totalCount: 0,
      input: '',
      text1: '确定删除',
      text2: '这篇文章吗?',
      size: 'medium',
      dialogVisible: false,
      dialogVisibleAdd: false,
      searchForm: {
        title: '',
        username: '',
        pageNum: 1,
        articleType: ''
      },
      tableData: [],
      labelPosition: 'right',
      options: [],
      addNewType: ''
    }
  },
  watch: {},
  mounted: function() {
    this.findArticleList()
    this.getTypeOptions()
  },
  methods: {
    delType(id) {
      delArticleType({ id: id }).then(() => {
        this.getTypeOptions()
      })
    },
    addArticleType() {
      addArticleType({ name: this.addNewType }).then(res => {
        if (res.code === 1) {
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          this.dialogVisibleAdd = false
          this.addNewType = ''
        } else {
          this.$message({
            type: 'error',
            message: res.data
          })
        }
      })
    },
    manageArticleType() {
      this.dialogVisible = true
    },
    getTypeOptions() {
      getTypeListAll().then(res => {
        this.options = res.data
      })
    },
    //  文章管理 查询所有文章
    findArticleList() {
      findArticleList(this.searchForm).then(res => {
        this.tableData = res.data.list
        this.totalCount = res.data.total
      })
    },
    //  用户管理 删除用户
    del(id) {
      delArticle({ articleId: id }).then(() => {
        this.findArticleList()
      })
    },
    cancel() {

    },
    //  查看文章
    check(data) {
      console.log(data)
      window.open('http://localhost:9528/#/article/' + data)
    },
    //  用户管理 编辑用户信息
    edit() {
      this.$store.dispatch('user/edit', this.editInfo).then(res => {
        alert('修改成功')
      })
    },
    handleClose() {
      this.dialogVisible = false
      this.dialogVisibleEdit = false
    },
    pageCurrentChange(val) {
      this.searchForm.pageNum = val
      this.findArticleList()
    },
    clear() {
      this.findArticleList()
    },
    changeHandle(value) {

    }
  }
}
</script>

<style lang="scss" scoped>
.container{
  margin-left: 15px;
  margin-top: 20px;
}
.el-card{
  margin-right: 10px;
}
.el-pagination{
  text-align: right;
  margin-top: 20px;
}
</style>
